package com.zjh.orderServer.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.zjh.common.R;
import com.zjh.entity.Order;
import com.zjh.orderServer.mapper.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * ClassName: OrderController
 * Description:
 * @Author zjh
 * @Create 2023/10/30 13:31
 */
@RestController
@RequestMapping("/order")
public class OrderController {
	@Autowired
	private OrderMapper orderMapper;
	
	
	@GetMapping("/findAllByState/{state}/{begin}/{len}")
	public R<List<Order>> findByState(@PathVariable Integer state,
	                                  @PathVariable Integer begin,
	                                  @PathVariable Integer len){
		Integer count = Math.toIntExact(orderMapper.selectCount(new LambdaQueryWrapper<Order>().eq(Order::getState, state)));
		return new R<List<Order>>(0,"",count,orderMapper.findAllByState(state,begin,len));
	}
	@GetMapping("/updateState/{id}/{aid}/{state}")
	public void updateState(@PathVariable Integer id,
	                        @PathVariable Integer aid,
	                        @PathVariable Integer state){
		orderMapper.updateState(id,aid,state);
	}
	
	
	@DeleteMapping("deleteByCondition/{uid}/{mid}/{aid}")
	public void deleteByCondition(@PathVariable(required = false) Integer uid,
	                              @PathVariable(required = false)Integer mid,
	                              @PathVariable(required = false)Integer aid){
		orderMapper.deleteByCondition(uid,mid,aid);
	}
	
	@PostMapping("save")
	public void save(@RequestBody Order order){
		orderMapper.saveOrder(order);
	}
	
	@GetMapping("findAllByUid/{uid}/{begin}/{len}")
	public R<List<Order>> findById(@PathVariable Integer uid,
	                        @PathVariable Integer begin,
	                        @PathVariable Integer len){
		return new R(0,"",orderMapper.countByUid(uid),orderMapper.findAllByUid(uid,begin,len));
	}
	
	
}
